var artist;
var letters = [
    'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
];
$(function () {
    artist = new Vue({
        el:'#artistController',
        data:{
            letters:[],
            letter:'',
            artists:{},
            list:[],
            param:{
                apply:false
            },
            isArt:_level,
            info:{
                realName:'',
                artHeadImg:'',
                artPhone:'',
                email:'',
                content:''
            }
        },
        methods:{
            loadData:function () {
                var that = this;
                sy.post('/web/artist/list',{
                    keyword:_keyword
                },function (data) {
                    that.letters = data.letters;
                    that.artists = data.data;
                    that.selectLetter('');
                })
            },
            selectLetter:function (letter) {
                var that=this;
                this.letter = letter;
                if (!letter){
                    this.list = [];
                    for (var key in this.artists){
                        for (var i  = 0; i < this.artists[key].length; i++){
                            var obj = this.artists[key][i];
                            if (obj.goodsImg){
                                obj.imgs = obj.goodsImg.split(',');
                                if (obj.imgs.length > 4){
                                    obj.imgs = obj.imgs.slice(0,4);
                                }
                            }
                            this.list.push(obj);
                        }
                    }
                }else{
                    this.list = this.artists[letter];
                    for (var i = 0; i < this.list.length; i++){
                        var obj = this.list[i];
                        if (obj.goodsImg){
                            obj.imgs = obj.goodsImg.split(',');
                            if (obj.imgs.length > 4){
                                obj.imgs = obj.imgs.slice(0,4);
                            }
                        }
                    }
                }
                this.list.forEach(function(item,index){
                    var itemText=item.artInfo;
                    if(itemText.length>75){
                        that.list[index].artInfo=itemText.slice(0,75)+'...'
                    }
                })
            },
            focus:function (item) {
                if (item.isFriend != 1){
                    sy.postApi('/api/user/addUserFocus',{
                        focusType:1,
                        id:item.id
                    },function (data) {
                        item.isFriend = 1;
                    })
                }else{
                    sy.postApi('/api/user/delUserFocus',{
                        focusType:1,
                        id:item.id
                    },function (data) {
                        item.isFriend = 0;
                    })
                }

            },
            apply:function (value) {
                this.param.apply = value;
            },
            updateImgs:function (img) {
                this.info.artHeadImg = img;
                sy.closeLoad();
            },
            uploadImg:function () {
                $("#img").trigger("click");
            },
            applyArtist:function () {
                var info = this.info;
                if (!info.realName){
                    sy.msg('请输入真实姓名');
                    return;
                }
                if (!info.artHeadImg){
                    sy.msg('请上传头像');
                    return;
                }
                if (!info.artPhone){
                    sy.msg('请输入手机号码');
                    return;
                }
                if (!info.email){
                    sy.msg('请输入常用邮箱');
                    return;
                }
                if (!info.content){
                    sy.msg('请输入个人履历');
                    return;
                }
                var that = this;
                sy.postApi('/api/userInfo/applyToArt',info,function (data) {
                    that.info = {
                        realName:'',
                        artHeadImg:'',
                        artPhone:'',
                        email:'',
                        content:''
                    };
                    that.apply(false);
                });
            }
        },
        created:function () {
            this.loadData();
        },
        mounted:function () {
            $("#img").bind("change",function(){
                if($(this).val() == '')return;
                sy.openLoad();
                $("#form1").submit();
            });
            //艺术家列表推荐的图片宽高一致
            var modelPercent=9/8;
            $('.artist_works li').each(function(index, item) {
                console.log(index,item)

                var picWidth = $(item).find('img').width();
                var picHeight = $(item).find('img').height();
                var picPercent = picWidth / picHeight;
                if (picPercent >= modelPercent) {
                    $(item).find('img').css({
                        'height': '160px'
                    });
                } else {
                    $(item).find('img').css({
                        'width': '100%'
                    });
                }
            });
        },
        updated:function () {
            
        }
    });
})


/**
 * 图片上传回调
 * @param id
 */
function callback(id) {
    artist.updateImgs(id);
}